package servlet;

import bean.User;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CheckUserServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CheckUserServlet doPost");

        String username = req.getParameter("username");
        System.out.println(username);

        resp.setContentType("text/html;charset=utf-8");
        if ("king".equals(username)) {
            //禁用king用户
            User king = new User(100, "king", "666", "king@sohu.com");
            String json = new Gson().toJson(king);
            //返回浏览器
            resp.getWriter().write(json);
        } else {
            //如果用户名可用，返回空串
            resp.getWriter().write("");
        }
    }
}
